package com.paic.custserv.utils.redis;

import java.io.Serializable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class RedisUtils implements Serializable { 
	private static final Logger logger = LoggerFactory.getLogger(RedisUtils.class);
	private static final long serialVersionUID = 1L;
	
	private JedisPool jedisPool;
    public void setJedisPool(JedisPool jedisPool) {
		this.jedisPool = jedisPool;
	}	
	/**  
     * 通过key获取 
     * @return 
     */  
    public Object get(String key) {  
    	Jedis jedis = null;
        try {
        	jedis = jedisPool.getResource();
        	return SerializeUtil.unserialize(jedis.get(key).getBytes("ISO-8859-1"));
        }catch(Exception e){
        	logger.error(e.getMessage(), e);
        } finally {
            if (jedis != null) {
                jedisPool.returnResource(jedis);
            }
        }
        
        return null;
    }  
	
	/**  
     * 新增 
     * 
     * @return 
     */  
    public boolean add(RedisObject robj) {  
    	Jedis jedis = null;
        try {
        	String key  = robj.getKey();
            String value = new String(SerializeUtil.serialize(robj.getValue()),"ISO-8859-1");
            jedis = jedisPool.getResource();
            jedis.setex(key, 1000, value); // 设置过期时间(毫秒)
        }catch(Exception e){
        	logger.error(e.getMessage(), e);
        	return false;
        } finally {
            if (jedis != null) {
                jedisPool.returnResource(jedis);
            }
        }
    	
        return true;
    }

    /**  
     * 删除 
     * 
     * @param key 
     */  
    public void delete(String key) {  
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            if(jedis.get(key) != null){
            	jedis.del(key);
            }
        }catch(Exception e){
        	logger.error(e.getMessage(), e);
        } finally {
            if (jedis != null) {
                jedisPool.returnResource(jedis);
            }
        }
    	
    }  
  
    /** 
     * 修改  
     * @param user 
     * @return  
     */  
    public boolean update(String key, RedisObject robj) {  
    	Jedis jedis = null;
        try {
        	if(get(key) != null){
                jedis = jedisPool.getResource();
                // 设置过期时间(毫秒)
                jedis.setex(key, 1000, new String(SerializeUtil.serialize(robj.getValue()),"ISO-8859-1"));
                
                // jedis.expire(key, 1000);	// 设置过期时间(毫秒)
        	}
        }catch(Exception e){
        	logger.error(e.getMessage(), e);
        	return false;
        } finally {
            if (jedis != null) {
                jedisPool.returnResource(jedis);
            }
        }
    	
        return true;
    }  
    //bzx 2015-03-27
    /**
     * 获取缓存的key
     * @param pre
     * @param key
     * @param suffix
     * @return
     */
	public String getCacheKey(String pre, Object key, String suffix) {
		StringBuilder buf = new StringBuilder();
		if (pre != null) {
			buf.append(pre);
		}
		buf.append(key);
		if (suffix != null) {
			buf.append(suffix);
		}
		return buf.toString();
	} 
    /**
     * 获取缓存的值
     * @param key
     * @return String
     */
    public String getKey(String key) {  
    	Jedis jedis = null;
        try {
        	jedis = jedisPool.getResource();
        	return jedis.get(key);
        }catch(Exception e){
        	logger.error(e.getMessage(), e);
        } finally {
            if (jedis != null) {
                jedisPool.returnResource(jedis);
            }
        }
        
        return null;
    } 
   /**
    * 保存/修改缓存
    * @param key
    * @param seconds
    * @param value
    * @return boolean
    */
    public boolean setKey(String key,int seconds,String value) {  
    	Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            if(seconds>0){
            	jedis.setex(key, seconds, value);
            }else{
            	jedis.set(key,value);
            }
        }catch(Exception e){
        	logger.error(e.getMessage(), e);
        	return false;
        } finally {
            if (jedis != null) {
                jedisPool.returnResource(jedis);
            }
        }
        return true;
    }
    /**
     * 删除缓存
     * @param key
     */
    public void deleteKey(String key) {  
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            if(jedis.get(key) != null){
            	jedis.del(key);
            }
        }catch(Exception e){
        	logger.error(e.getMessage(), e);
        } finally {
            if (jedis != null) {
                jedisPool.returnResource(jedis);
            }
        }
    	
    }  
}  